package com.test.rpc.customer;

import com.test.rpc.ComputeEntity;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ComputeLocal {
    public int compute(int a, int b) throws IOException {
        //分配地址
//        List<String> remoteIP=localIpList("ComputeAdd");
        Socket socket = new Socket("127.0.0.1", 9090);
        //序列化对象
        ComputeEntity entity = new ComputeEntity(a, b);
        //输出到真正处理的方法上
        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
        outputStream.writeObject(entity);

        //接受返回结果
        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
        Object response = new Object();
        try {
            response = inputStream.readObject();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            log.error("error :{}", e);
        }
        return (int) response;
    }

    private List<String> localIpList(String methodName) {
        List<String> ips = new ArrayList<>();
        ips.add("127.0.0.1");
        return ips;
    }
}
